<?php
/**
 * Created by PhpStorm.
 * User: 123
 * Date: 2020/7/21
 * Time: 13:28
 */

namespace app\service;


use phpmailer\BaseException;
use think\Db;

class SpeareaService extends BaseService
{
    /**
     * 获取专区
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年7月21日13:29:33
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
    public static function AreaList($params = [])
    {
        // 获取专区
        $params['table']='bizpro_spearea';
        $data=self::DataList($params);
//        $where = empty($params['where']) ? [] : $params['where'];
//        $field = empty($params['field']) ? '*' : $params['field'];
//        $order_by = empty($params['order_by']) ? 'id desc' : trim($params['order_by']);
//        $page = $params['page'] ? true : false;
//        $number = isset($params['number']) ? intval($params['number']) : 10;
//        $m = isset($params['m']) ? intval($params['m']) : 0;
//        $n = isset($params['n']) ? intval($params['n']) : 10;
//        $data = Db::name('bizpro_spearea')->field($field)->where($where)->order($order_by);
//        if($page)
//        {
//            $data=$data->paginate($number, false, ['query' => request()->param()]);
//            $data=$data->toArray()['data'];
//
//        }else{
//            $data=$data->limit($m, $n)->select();
//        }

        return self::GoodsCategoryDataDealWith($data,$params);

    }




    /**
     * 商品专区数据处理
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年7月21日13:29:33
     * @desc    description
     * @param   [array]          $data [商品专区数据 二维数组]
     */
    private static function GoodsCategoryDataDealWith($data,$params){
        if(!empty($data) && is_array($data))
        {
            foreach($data as &$v)
            {
                if(is_array($v))
                {

                    if(isset($v['imgurl']))
                    {
                        $is_local=true;
                        if($params['is_client'])
                        {
                            $is_local=false;
                        }
                        $v['src_imgurl'] = ResourceService::AttachmentPathViewHandle($v['imgurl'],$is_local);
                    }
                    //专区等级 上级专区
                    if(isset($v['pid']))
                    {
                      if($v['pid']){
                          $v['level']='二级专区';
                          $v['level_up']=Db::name('bizpro_spearea')->where('id='.$v['pid'])->value('title');
                      }else{
                          $v['level']='一级专区';
                          $v['level_up']='--';
                      }
                    }



                }
            }
        }
        return DataReturn('获取成功',0,$data);
    }

    /**
     * 总数
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年7月21日13:29:33
     * @desc    description
     * @param    [array]          $where [条件]
     */
    public static function AreaTotal($where){
        return (int) Db::name('bizpro_spearea')->where($where)->count();
    }

    /**
     * 专区数据校验
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年7月21日13:29:33
     * @param    [array]          $params [输入参数]
     */
    public static function VaryData($params)
    {
        // 请求参数
        $p = [
            [
                'checked_type' => 'empty',
                'key_name' => 'title',
                'error_msg' => '名称不能为空',
                'error_code' => 20002,
            ],
            [
                'checked_type' => 'empty',
                'key_name' => 'imgurl',
                'error_msg' => '缩略图不能为空',
                'error_code' => 20003,
            ],
            [
                'checked_type' => 'empty',
                'key_name' => 'position',
                'error_msg' => '显示位置不能为空',
                'error_code' => 20004,
            ],


        ];

        $ret=ParamsChecked($params, $p);

        if(true !== $ret)
        {

            //$error_arr[0]  $error_arr[1]
            $error_arr = explode(',', $ret);
            throw new BaseException(['code'=>403 ,'errorCode'=>$error_arr[1],'msg'=>$error_arr[0],'status'=>false,'debug'=>false ]);
        }

        //位置重复判断
        if ($params['pid']<1){
            $check_data = [
                'where' => [['position', '=', $params['position']]],
                'table' => 'bizpro_spearea',
                'params' => $params,
                'msg' => '位置不能重复',
                'error_code' => 20001,
            ];
            self::CheckTitle($check_data);
        }

        $check_data = [
            'where' => [['title', '=', $params['title']]],
            'table' => 'bizpro_spearea',
            'params' => $params,
            'msg' => '同专区内名称不能重复',
            'error_code' => 20005,
        ];
        self::CheckTitle($check_data);
        return true ;
    }

    /**
     * 位置重复判断
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年7月21日15:20:43
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
    public static function CheckTitle($params){
        $data = Db::name($params['table'])->where($params['where'])->find();
        $cond1=!empty($data)&&empty($params['params']['id']);
        $cond2=!empty($data)&&isset($params['params']['id'])&&intval($data['id'])!=$params['params']['id'];
        $error=0;
        //添加数据
        if($cond1)
        {
            //专区名称校验
            if(isset($params['params']['pid']))
            {
                //统一专区下 名称不得相同
                if($data['pid']==$params['params']['pid'])
                {
                    $error=1;
                }
            }
            $error=1;
        }
        //编辑数据
        if($cond2)
        {
            //统一专区下 名称不得相同
            if($data['pid']==$params['params']['pid'])
            {
                $error=1;
            }
        }
        if($error)
        {
            throw new BaseException(['code'=>403 ,'errorCode'=>$params['error_code'],'msg'=>$params['msg'],'status'=>false,'debug'=>false]);

        }
        return true;
    }


    /**
     * 数据处理
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime 2020年7月21日15:24:57
     * @param    [array]          $params [输入参数]
     */
    public static function HandleData($params){
        $data = $params;

        //删除多余字段
        unset($data['thumb_img']);
        $data['imgurl']=str_replace('/upload/','',$data['imgurl']);
        return $data;
    }

    /**
     * 专区保存
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime 2020年7月21日15:25:02
     * @param    [array]          $params [输入参数]
     */
    public static  function SaveSpe($data,$params){

        // 添加/编辑
        if (empty($params['id']))
        {
            $cate_id = Db::name('bizpro_spearea')->insertGetId($data);
            if (!$cate_id) {

                throw new BaseException(['code'=>403 ,'errorCode'=>'30006','msg'=>'添加专区失败','status'=>false,'debug'=>false]);
            }
        } else {
            $data['update_time'] = TIMESTAMP;
            //($data);exit;
            $re = Db::name('bizpro_spearea')->where(['id' => intval($params['id'])])->update($data);
            if (!$re)
            {
                throw new BaseException(['code'=>403 ,'errorCode'=>'30007','msg'=>'修改专区失败','status'=>false,'debug'=>false]);
            }
        }
    }
}